import 'package:enter_prise_web/models/group.dart';
import 'package:enter_prise_web/util/MyDio.dart';
import 'package:enter_prise_web/widgets/selectWidget.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';

class settingForm extends StatefulWidget {
  const settingForm({super.key});

  @override
  State<settingForm> createState() => _settingFormState();
}

class _settingFormState extends State<settingForm> with WidgetsBindingObserver {
  final fontSizes = 17.0;
  final MyDio myDio = MyDio();

  final GlobalKey<FormState> _formKey = GlobalKey();
  late String? groupName;
  late String objectName;
  late String oid;
  late int price;
  late String commentary;

  @override
  void dispose() {
    super.dispose();
    WidgetsBinding.instance.removeObserver(this);
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    groupName = group.getGid("管理员组");
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  void didChangeMetrics() {
    // TODO: implement didChangeMetrics
    super.didChangeMetrics();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      setState(() {
        if (MediaQuery.of(context).viewInsets.bottom == 0) {
          //键盘收回
        } else {
          //键盘弹出
        }
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      color: Colors.transparent,
      child: SizedBox(
        height: 450 + MediaQuery.of(context).viewInsets.bottom,
        width: double.infinity,
        child: Form(
          key: _formKey,
          child: Column(mainAxisAlignment: MainAxisAlignment.start, children: [
            Container(
              margin: const EdgeInsets.only(top: 8.0),
              width: 20,
              height: 6,
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(10), color: Colors.grey),
            ),
            const SizedBox(
              height: 20,
            ),
            Container(
              width: double.infinity,
              padding: const EdgeInsets.all(8.0),
              child: Row(
                children: [
                  Padding(
                    padding: const EdgeInsets.only(
                      left: 18,
                    ),
                    child: Text(
                      "组别：",
                      style: TextStyle(fontSize: fontSizes),
                    ),
                  ),
                  Container(
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(5),
                      border: Border.all(color: Colors.grey),
                    ),
                    padding: const EdgeInsets.all(8.0),
                    child: SelectWidget(
                      valueChanged: (value) {
                        groupName = value;
                      },
                      items: [
                        MenuItem(
                            label: "管理员组",
                            value: group.getGid("管理员组"),
                            checked: false),
                        MenuItem(
                            label: "裁剪车间",
                            value: group.getGid("裁剪车间"),
                            checked: false),
                        MenuItem(
                            label: "张送真",
                            value: group.getGid("张送真"),
                            checked: false),
                        MenuItem(
                            label: "包装组",
                            value: group.getGid("包装组"),
                            checked: false),
                        MenuItem(
                            label: "销售员",
                            value: group.getGid("销售员"),
                            checked: false),
                        MenuItem(
                            label: "刘洋",
                            value: group.getGid("刘洋"),
                            checked: false),
                      ],
                    ),
                  ),
                ],
              ),
            ),
            Container(
              width: double.infinity,
              margin: const EdgeInsets.only(
                  left: 8.0, top: 8.0, bottom: 8.0, right: 20.0),
              child: Row(
                children: [
                  Padding(
                    padding: const EdgeInsets.only(
                      left: 18,
                    ),
                    child: Text(
                      "名称：",
                      style: TextStyle(fontSize: fontSizes),
                    ),
                  ),
                  Expanded(
                    child: TextFormField(
                      decoration: const InputDecoration(
                        labelText: "请输入工件名称",
                        alignLabelWithHint: true,
                        contentPadding: EdgeInsets.symmetric(
                            vertical: 10.0, horizontal: 16.0),
                        border: OutlineInputBorder(),
                      ),
                      onSaved: (newValue) {
                        objectName = newValue!;
                        print(objectName);
                      },
                    ),
                  ),
                ],
              ),
            ),
            Container(
              width: double.infinity,
              margin: const EdgeInsets.only(
                  left: 8.0, top: 8.0, bottom: 8.0, right: 20.0),
              child: Row(
                children: [
                  Padding(
                    padding: const EdgeInsets.only(
                      left: 18,
                    ),
                    child: Text(
                      "编号：",
                      style: TextStyle(fontSize: fontSizes),
                    ),
                  ),
                  Expanded(
                    child: TextFormField(
                      decoration: const InputDecoration(
                        labelText: "请输入工件编号",
                        hintText: "编号自己输哦,不要重复，重复了我会提醒你",
                        alignLabelWithHint: false,
                        contentPadding: EdgeInsets.symmetric(
                            vertical: 10.0, horizontal: 16.0),
                        border: OutlineInputBorder(),
                      ),
                      onSaved: (newValue) {
                        oid = newValue!;
                      },
                    ),
                  ),
                ],
              ),
            ),
            Container(
              width: double.infinity,
              margin: const EdgeInsets.only(
                  left: 8.0, top: 8.0, bottom: 8.0, right: 20.0),
              child: Row(
                children: [
                  Padding(
                    padding: const EdgeInsets.only(
                      left: 18,
                    ),
                    child: Text(
                      "价格：",
                      style: TextStyle(fontSize: fontSizes),
                    ),
                  ),
                  Expanded(
                    child: TextFormField(
                      decoration: const InputDecoration(
                        labelText: "请输入价格",
                        hintText: "价格定太高不利于销量",
                        alignLabelWithHint: true,
                        contentPadding: EdgeInsets.symmetric(
                            vertical: 10.0, horizontal: 16.0),
                        border: OutlineInputBorder(),
                      ),
                      onSaved: (value) {
                        price = int.parse(value!);
                      },
                    ),
                  ),
                ],
              ),
            ),
            Container(
              width: double.infinity,
              margin: const EdgeInsets.only(
                  left: 8.0, top: 8.0, bottom: 8.0, right: 20.0),
              child: Row(
                children: [
                  Padding(
                    padding: const EdgeInsets.only(
                      left: 18,
                    ),
                    child: Text(
                      "备注：",
                      style: TextStyle(fontSize: fontSizes),
                    ),
                  ),
                  Expanded(
                    child: TextFormField(
                      decoration: const InputDecoration(
                        labelText: "有备注吗？",
                        alignLabelWithHint: true,
                        contentPadding: EdgeInsets.symmetric(
                            vertical: 10.0, horizontal: 16.0),
                        border: OutlineInputBorder(),
                      ),
                      onSaved: (newValue) {
                        commentary = newValue!;
                      },
                    ),
                  ),
                ],
              ),
            ),
            Container(
              margin: const EdgeInsets.only(top: 25),
              width: 100,
              height: 40,
              decoration: BoxDecoration(
                color: Colors.orange,
                borderRadius: BorderRadius.circular(10),
              ),
              child: TextButton(
                  onPressed: () async {
                    if (_formKey.currentState!.validate()) {
                      _formKey.currentState!.save();
                      var response = await myDio.requestData("/item/add", params: {
                        "objectName": objectName,
                        "oid": oid,
                        "gid": groupName,
                        "value": price,
                        "commentary": commentary,
                      },method: 'POST');
                      print(response.data);
                      if (response.statusCode == 200 && response.data == "ok") {
                        Fluttertoast.showToast(
                            webPosition: "center", msg: "添加成功");
                        Get.back();
                      } else {
                        Fluttertoast.showToast(
                            webPosition: "center", msg: response.data);
                      }
                    }
                  },
                  child: const Text(
                    "添加",
                    style: TextStyle(color: Colors.white, fontSize: 18),
                  )),
            ),
          ]),
        ),
      ),
    );
  }
}
